# 斐波那契数列
def fib(n):
    if n < 2:
        return n
    else:
        return fib(n - 2) + fib(n - 1)


print(fib(7))


# 快排
def quick(qlist):
    if len(qlist) <= 0:
        return qlist
    else:
        qb = qlist[0]
        qa = quick([l for l in qlist[1:] if l <= qb])
        qc = quick([m for m in qlist[1:] if m > qb])
        return qa + [qb] + qc


print(quick([34, 2, 5, 25, 36]))


# 随机插入排序
def insertQuick(qlist):
    for i in range(1, len(qlist)):
        key = qlist[i]
        j = i - 1
        while j >= 0 and key < qlist[j]:
            qlist[j + 1] = qlist[j]
            j -= 1
            qlist[j + 1] = key
    return qlist


print(insertQuick([23, 3, 42, 345, 2]))


# 99乘法表
def n_n():
    for i in range(1, 10):
        for j in range(1, i + 1):
            print(f'{j}X{i}={j * i} ', end=' ')
        print('\t')
    return "以上是99乘法表！"


print(n_n())


# 统计字符串中出现次数最多的字符和次数
def strCount():
    s1 = "ASDddaADASAd"
    d1 = {}
    for s in s1:
        if s not in d1:
            d1[s] = 1
        else:
            d1[s] += 1

    for k, v in d1.items():
        if v == max(d1.values()):
            print(k, v)


print(strCount())


# Switch语句
def switch():
    s = {"k1": "2", "k2": 3, "k3": {3, 5}}
    print("switch语句：", s["k1"])


switch()


# filter()筛选功能
def fname(n):
    if n.startswith('张'):
        return True


print(list(filter(fname, ['张章', "曹丽", "薛云非", "张娴洁"])))


# print(fli.__next__())

# map()映射方法
def add(i):
    i += 1
    return i


print(list(map(add, [1, 2, 3, 4, 5, 6, 7])))


# 获取list索引位置
def l1():
    f1 = [1, 2, 3, 4, 5, 6]
    print(f1.index(3))


l1()


# 判断字符串中大写的几个，小写的几个
def str1():
    s1 = "ASDAdasdadaAdsa"
    d1 = 0
    x1 = 0
    for s in s1:
        if s.isupper():
            d1 += 1
        else:
            x1 += 1
    print(f'大写的{d1}个, 小写的{x1}个！')


str1()


# 修改元组内部元素不可修改的元素
def tp():
    t1 = (1, 2, "三", ["w", 3, "2"])
    t1[3].extend([22, 33, 44])
    return t1


print(tp())


# 列表推导式
def l():
    l1 = [i for i in range(10)]
    print(l1)


l()
# 集合推导式
print("集合推导式", {i for i in range(10)})
# 字典推导式
l1 = [22, 33, 44]
print({i: l1[i] for i in range(len(l1))})


# 函数嵌套
def wapper():
    a = 123

    def inner():
        nonlocal a
        a = 456
        print(a)
        return a

    return inner


# 迭代器
l2 = [123, 234, 345, 456]
ln = l2.__iter__()
ln.__next__()
print(ln.__next__())


# 生成器
def y():
    a = 3
    b = 4
    yield b
    print(a)
    yield a + 1


yy = y()
print(yy.__next__())
print(yy.__next__())


# 装饰器
def wapper1(name):
    a = 2

    def inner(*args, **kwargs):
        b = 23
        cc = name(*args, **kwargs)  # 执行目标函数的return结果
        print("执行内部函数完成！")
        return cc, "内部函数"

    return inner


@wapper1  # game = wapper1(game)
def game(username, password):
    print(f"用户名是{username}，密码是{password}")
    print("登录用户成功！")
    return "开始游戏！"


print(game(username="周抗抗", password="123456"))
